package com.shixun.six;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.util.Properties;

public class Testdbms {
    @Test//注解
    public void testdbms() {
        try {
            Connection conn = JDBCdbms.getConnection();
            System.out.println(conn);
            Statement stat = conn.createStatement();
            String sql = "select * from student6";//查询
            // String sql="delete from student3 where id=?";//删除
            // String sql="insert into student3 (id,name,age) values (?,?,?)";//插入数据
            // String sql="update student3 set name='李四' where id=2 ";//修改数据
            ResultSet rs = stat.executeQuery(sql);
            PreparedStatement ps = conn.prepareStatement(sql);
            //  ps.setInt(1,4);
            //  ps.setString(2,"zz");
            //  ps.setInt(3,22);
            //  ps.executeUpdate();

            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String age = rs.getString("age");
                System.out.println(id + " " + name + " " + age + " ");
            }
            rs.close();
            stat.close();
            conn.close();
            ps.close();//数据库内容
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Test
    public void testDruid() {
        try {
            Properties pro = new Properties();
            ClassLoader classLoader = JDBCdbms.class.getClassLoader();
            URL furl = classLoader.getResource("druid.properties");
            String path = furl.getPath();
            pro.load(new FileInputStream(path));
            DataSource ds = DruidDataSourceFactory.createDataSource(pro);
            Connection conn = ds.getConnection();
            System.out.println(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void testljdbc() {
        try {
            Connection conn= LJDBCUtils.getConnection();
            System.out.println(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
